home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP15 / POPPRNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.6 KB  |  177 lines

  1. /*----------------------------------------------
  2.    POPPRNT.C -- Popup Editor Printing Functions
  3.   ----------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include <string.h>
  8. #include "poppad.h"
  9.  
  10. BOOL bUserAbort ;
  11. HWND hDlgPrint ;
  12.  
  13. BOOL CALLBACK PrintDlgProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  14.      {
  15.      switch (msg)
  16.           {
  17.           case WM_INITDIALOG :
  18.                EnableMenuItem (GetSystemMenu (hDlg, FALSE), SC_CLOSE,
  19.                                                             MF_GRAYED) ;
  20.                return TRUE ;
  21.  
  22.           case WM_COMMAND :
  23.                bUserAbort = TRUE ;
  24.                EnableWindow (GetParent (hDlg), TRUE) ;
  25.                DestroyWindow (hDlg) ;
  26.                hDlgPrint = 0 ;
  27.                return TRUE ;
  28.           }
  29.      return FALSE ;
  30.      }          
  31.  
  32. BOOL CALLBACK AbortProc (HDC hPrinterDC, int iCode)
  33.      {
  34.      MSG msg ;
  35.  
  36.      while (!bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  37.           {
  38.           if (!hDlgPrint || !IsDialogMessage (hDlgPrint, &msg))
  39.                {
  40.                TranslateMessage (&msg) ;
  41.                DispatchMessage (&msg) ;
  42.                }
  43.           }
  44.      return !bUserAbort ;
  45.      }
  46.  
  47. BOOL PopPrntPrintFile (HINSTANCE hInst, HWND hwnd, HWND hwndEdit, 
  48.                                                    LPSTR szTitleName)
  49.      {
  50.      static DOCINFO  di = { sizeof (DOCINFO), "", NULL } ;
  51.      static PRINTDLG pd ;
  52.      BOOL            bSuccess ;
  53.      LPCTSTR         pstrBuffer ;
  54.      int             yChar, iCharsPerLine, iLinesPerPage, iTotalLines,
  55.                      iTotalPages, iPage, iLine, iLineNum ;
  56.      TEXTMETRIC      tm ;
  57.      WORD            iColCopy, iNoiColCopy ;
  58.  
  59.      pd.lStructSize         = sizeof (PRINTDLG) ;
  60.      pd.hwndOwner           = hwnd ;
  61.      pd.hDevMode            = NULL ;
  62.      pd.hDevNames           = NULL ;
  63.      pd.hDC                 = NULL ;
  64.      pd.Flags               = PD_ALLPAGES | PD_COLLATE | PD_RETURNDC ;
  65.      pd.nFromPage           = 0 ;
  66.      pd.nToPage             = 0 ;
  67.      pd.nMinPage            = 0 ;
  68.      pd.nMaxPage            = 0 ;
  69.      pd.nCopies             = 1 ;
  70.      pd.hInstance           = NULL ;
  71.      pd.lCustData           = 0L ;
  72.      pd.lpfnPrintHook       = NULL ;
  73.      pd.lpfnSetupHook       = NULL ;
  74.      pd.lpPrintTemplateName = NULL ;
  75.      pd.lpSetupTemplateName = NULL ;
  76.      pd.hPrintTemplate      = NULL ;
  77.      pd.hSetupTemplate      = NULL ;
  78.  
  79.      if (!PrintDlg (&pd))
  80.           return TRUE ;
  81.  
  82.      iTotalLines = (short) SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0L) ;
  83.  
  84.      if (iTotalLines == 0)
  85.           return TRUE ;
  86.  
  87.      GetTextMetrics (pd.hDC, &tm) ;
  88.      yChar = tm.tmHeight + tm.tmExternalLeading ;
  89.  
  90.      iCharsPerLine = GetDeviceCaps (pd.hDC, HORZRES) / tm.tmAveCharWidth ;
  91.      iLinesPerPage = GetDeviceCaps (pd.hDC, VERTRES) / yChar ;
  92.      iTotalPages   = (iTotalLines + iLinesPerPage - 1) / iLinesPerPage ;
  93.  
  94.      pstrBuffer = (LPCTSTR) HeapAlloc (GetProcessHeap (), 
  95.                                        HEAP_NO_SERIALIZE, iCharsPerLine + 1) ;
  96.  
  97.      EnableWindow (hwnd, FALSE) ;
  98.  
  99.      bSuccess   = TRUE ;
  100.      bUserAbort = FALSE ;
  101.  
  102.      hDlgPrint = CreateDialog (hInst, (LPCTSTR) "PrintDlgBox", hwnd, PrintDlgProc) ;
  103.      SetDlgItemText (hDlgPrint, IDD_FNAME, szTitleName) ;
  104.  
  105.      SetAbortProc (pd.hDC, AbortProc) ;
  106.  
  107.      GetWindowText (hwnd, (PTSTR) di.lpszDocName, sizeof (PTSTR)) ;
  108.  
  109.      if (StartDoc (pd.hDC, &di) > 0)
  110.           {
  111.           for (iColCopy = 0 ;
  112.                iColCopy < ((WORD) pd.Flags & PD_COLLATE ? pd.nCopies : 1) ;
  113.                iColCopy++)
  114.                {
  115.                for (iPage = 0 ; iPage < iTotalPages ; iPage++)
  116.                     {
  117.                     for (iNoiColCopy = 0 ;
  118.                          iNoiColCopy < (pd.Flags & PD_COLLATE ? 1 : pd.nCopies) ;
  119.                          iNoiColCopy++)
  120.                          {
  121.  
  122.                          if (StartPage (pd.hDC) < 0)
  123.                               {
  124.                               bSuccess = FALSE ;
  125.                               break ;
  126.                               }
  127.  
  128.                          for (iLine = 0 ; iLine < iLinesPerPage ; iLine++)
  129.                               {
  130.                               iLineNum = iLinesPerPage * iPage + iLine ;
  131.  
  132.                               if (iLineNum > iTotalLines)
  133.                                    break ;
  134.  
  135.                               *(int *) pstrBuffer = iCharsPerLine ;
  136.  
  137.                               TextOut (pd.hDC, 0, yChar * iLine, pstrBuffer,
  138.                                    (int) SendMessage (hwndEdit, EM_GETLINE,
  139.                                    (WPARAM) iLineNum, (LPARAM) pstrBuffer)) ;
  140.                               }
  141.  
  142.                          if (EndPage (pd.hDC) < 0)
  143.                               {
  144.                               bSuccess = FALSE ;
  145.                               break ;
  146.                               }
  147.  
  148.                          if (bUserAbort)
  149.                               break ;
  150.                          }
  151.  
  152.                     if (!bSuccess || bUserAbort)
  153.                          break ;
  154.                     }
  155.  
  156.                if (!bSuccess || bUserAbort)
  157.                     break ;
  158.                }
  159.           }
  160.      else
  161.           bSuccess = FALSE ;
  162.  
  163.      if (bSuccess)
  164.           EndDoc (pd.hDC) ;
  165.  
  166.      if (!bUserAbort)
  167.           {
  168.           EnableWindow (hwnd, TRUE) ;
  169.           DestroyWindow (hDlgPrint) ;
  170.           }
  171.  
  172.      HeapFree (GetProcessHeap (), 0, (LPVOID) pstrBuffer) ;
  173.      DeleteDC (pd.hDC) ;
  174.  
  175.      return bSuccess && !bUserAbort ;
  176.      }
  177.